﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FitProtocolLibrary
{
    public class FitField
    {
        public byte FieldDefinitionNumber;
        public string Name;
        public byte Size;
        public BaseType BaseType;
        public byte[] Value;

        public byte[] FieldDefinitionData
        {
            get
            {
                byte[] fieldDefinitionData = new byte[3];
                fieldDefinitionData[0] = this.FieldDefinitionNumber;
                fieldDefinitionData[1] = this.Size;
                fieldDefinitionData[2] = (byte)this.BaseType;
                return fieldDefinitionData;
            }
            set
            {
                if (value.Length != 3)
                    throw new ArgumentOutOfRangeException("FieldDefinitionData", "FieldDefinitionData must be 3 bytes.");
                this.FieldDefinitionNumber = value[0];
                this.Size = value[1];
                this.BaseType = (BaseType)value[2];
            }
        }

        public FitField(FitProfileMessage profile, byte[] fieldDef)
        {
            if (fieldDef.Length != 3)
                throw new ArgumentOutOfRangeException("fieldDef", "Field definition must be 3 bytes long.");
            this.FieldDefinitionNumber = fieldDef[0];
            this.Size = fieldDef[1];
            this.BaseType = (BaseType)fieldDef[2];
            this.Value = null;
            this.Name = profile.Fields.SingleOrDefault(q => q.Number == this.FieldDefinitionNumber).Name;
        }

        public FitField(FitField fieldDef)
        {
            this.FieldDefinitionNumber = fieldDef.FieldDefinitionNumber;
            this.Name = fieldDef.Name;
            this.Size = fieldDef.Size;
            this.BaseType = fieldDef.BaseType;
        }
    }
}
